﻿using DeepEarth.Core;
using System.Collections.Generic;
using System.Linq;
using DeepEarth.Core.Data;
using System;
using DeepEarth.Map.Core.Clustering;
namespace DeepEarth.Map.Core.Filters
{
    public class BoundsChecker
    {
        private LocationRectangle viewportRectangle;
        public LocationRectangle ViewportRectangle
        {
            get
            {
                return viewportRectangle;
            }
            set
            {
                viewportRectangle = value;
            }
        }

        public Dictionary<ClusterViewModel, ClusterViewModel> Filter(List<ClusterViewModel> viewModels)
        {
            if (viewportRectangle == null)
            {
                throw new Exception("Viewport Rectangle must be set before this filter can be executed.");
            }

            Dictionary<ClusterViewModel, ClusterViewModel> inBounds = new Dictionary<ClusterViewModel, ClusterViewModel>();

            foreach (ClusterViewModel vm in viewModels)
            {
                if (vm.IsActive && vm.WrappedViewModelVisible)
                {
                    if (vm.BoundingBox != null)
                    {
                        if (viewportRectangle.Collides(vm.BoundingBox))
                        {
                            inBounds.Add(vm, vm);
                        }
                    }
                    else if (vm.Position != null)
                    {
                        if (viewportRectangle.Contains(vm.Position))
                        {
                            inBounds.Add(vm, vm);
                        }
                    }
                    // Position is null = always visible
                    else
                    {
                        inBounds.Add(vm, vm);
                    }
                }
            }

            return inBounds;
        }
    }
}
